package azku;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.SharedPreferences;
import azku.entitys.EGroupType;
import azku.photoviews.DataSetting;

public class XSharedPreferences {
	private static XSharedPreferences _Instance;

	public static XSharedPreferences It() {
		if (_Instance == null) {
			_Instance = new XSharedPreferences();
		}
		return _Instance;
	}

	private SharedPreferences _sharedpreferences;

	private XSharedPreferences() {
		_sharedpreferences = MyApplication.getInstance().getSharedPreferences(
				DataSetting.S_SHARED_GROUPTYPESFILE, 0);
	}

	public List<EGroupType> GetListGroupType() {
		List<EGroupType> ret = null;
		String grouptypes = _sharedpreferences.getString(
				DataSetting.S_SHARED_GROUPTYPENODEKEY, null);
		if (grouptypes == null || grouptypes.equals("")) {
			ret = null;
		} else {
			try {
				JSONArray json = new JSONArray(grouptypes);
				if (json != null && json.length() > 0) {
					ret = new ArrayList<EGroupType>();
					for (int i = 0; i < json.length(); i++) {
						EGroupType eitem = new EGroupType();
						try {
							eitem.bindParams(json.getJSONObject(i));
							ret.add(eitem);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
				ret = null;
			}
		}
		return ret;
	}

	public void SaveListGroupType(JSONArray json) {
		if (json == null || json.length() <= 0)
			return;
		String strPut = json.toString();
		if (strPut == null || strPut.equals(""))
			return;
		SharedPreferences.Editor editor = _sharedpreferences.edit();
		editor.putString(DataSetting.S_SHARED_GROUPTYPENODEKEY, strPut);
		editor.commit();
	}
}
